/**
 * @param {number[]} height
 * @return {number}
 */

// 半暴力解法，用全暴力解法会直接超时
var maxArea1 = function (height) {
  let num = 0;
  for (let index1 = 0; height.length > index1 + 1; index1++) {
    for (let index2 = height.length - 1;index2 > index1; index2--) {
      const item1 = height[index1];
      const item2 = height[index2];
      const nowNum = (index2 - index1) * Math.min(item1, item2);
      if (nowNum > num) num = nowNum;
      if (item2 >= item1) break;
    }
  }
  return num;
};

// 双指针解法
var maxArea = function (height) {
  let num = 0;
  let startIndex = 0;
  let endIndex = height.length - 1;
  while (startIndex !== endIndex) {
    const start = height[startIndex];
    const end = height[endIndex];
    const nowNum = (endIndex - startIndex) * Math.min(start, end);
    if (nowNum > num) num = nowNum;
    if (start >= end) {
      endIndex--
    } else {
      startIndex++
    }
  }

  return num;
};

console.log(maxArea([1, 8, 6, 2, 5, 4, 8, 3, 7]));
